package com.skivingcloud.admin.sys.controller;


import java.util.Map;

import com.skivingcloud.admin.sys.entity.SysMsgStream;
import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.admin.sys.service.ISysMsgStreamService;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.common.utils.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 消息流水 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/sys/msgStream")
public class SysMsgStreamController {

	private static Logger log = LoggerFactory.getLogger(SysMsgStreamController.class);

	@Autowired
	private ISysMsgStreamService sysMsgStreamService;

	/**
	 * 发送消息接口
	 * 
	 * @param params {"senderId":发送用户id(不要用员工编号）,"pageParam":页面参数,"msgCode":消息定义编码,"businessId":业务id,"msgContext":消息内容,"msgContextParam":消息内容参数}
	 * @return
	 */
	@PostMapping("/sendMsg")
	public boolean sendMsg(@RequestBody Map<String, Object> params) {
		try {
			if (StringUtils.isBlank(params.get("senderId"))) {
				try {
					User user = UserUtil.getUser() == null ? UserUtil.getSystem() : UserUtil.getUser();
					params.put("senderId", user.getId());
				} catch (Exception e) {
					params.put("senderId", UserUtil.getSystem().getId());
				}
			}
			return sysMsgStreamService.sendMsg(params);
		} catch (Exception e) {
			log.error("发送消息失败", e);
			return false;
		}
	}

	/**
	 * 所有配置列表
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		PageUtils page = sysMsgStreamService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 当前登录用户的未读消息
	 * 
	 * @param params
	 * @return
	 */
	@PostMapping("/queryUndealMsg")
	public R queryUndealMsg(@RequestBody Map<String, Object> params) {
		User user = UserUtil.getUser();
		PageUtils page = sysMsgStreamService.queryUndealMsg(params, user);
		return R.ok().put("undealMsg", page);
	}

	/**
	 * 配置信息
	 * 
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		SysMsgStream msgStream = sysMsgStreamService.selectMapById(id);
		return R.ok().put("msgStream", msgStream);
	}

	/**
	 * 保存配置
	 */
	@PostMapping("/save")
	public R save(@RequestBody SysMsgStream msgStream) {
		sysMsgStreamService.saveMe(msgStream);
		return R.ok();
	}

	/**
	 * 修改配置
	 */
	@PostMapping("/update")
	public R update(@RequestBody SysMsgStream msgStream) {
		sysMsgStreamService.update(msgStream);
		return R.ok();
	}

	/**
	 * 删除配置
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		sysMsgStreamService.deleteBatch(ids);
		return R.ok();
	}

	/**
	 * 标为已读
	 */
	@PostMapping("/deal")
	public R deal(@RequestBody String[] ids) {
		sysMsgStreamService.deal(ids);
		return R.ok();
	}

	/**
	 * 查询待处理的消息数量
	 * 
	 * @param userId
	 * @return
	 */
	@GetMapping("/getUndealMsgNum/{userId}")
	public R getUndealMsgNum(@PathVariable("userId") String userId) {
		int msgNum = sysMsgStreamService.getUndealMsgNum(userId);
		return R.ok().put("msgNum", msgNum);
	}

}
